package net.wanho.shop.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.mapper.SupplyMapper;
import net.wanho.shop.po.Member;
import net.wanho.shop.po.Supply;
import net.wanho.shop.service.SupplyService;
import net.wanho.shop.vo.PageVo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2025/1/6 17:59
 * @version: 1.0
 */

@Service
@RequiredArgsConstructor
public class SupplyServiceImpl implements SupplyService {

    private final SupplyMapper supplyMapper;

    @Override
    public PageVo selectAll(int pageNum, int pageSize, Supply supply) {
        PageHelper.startPage(pageNum, pageSize);
        List<Supply> supplies = supplyMapper.selectAll();
        PageInfo<Supply> pageInfo = new PageInfo<>(supplies);
        PageVo pageVo = new PageVo(pageInfo.getTotal(), pageNum);
        pageVo.put("supplies", pageInfo.getList());
        return pageVo;
    }

    @Override
    public void addSupply(Supply supply) {
        supplyMapper.addSupply(supply);
    }

    @Override
    public void deleteSupply(Integer supplyId) {
        // 判断该id是否有商品
        if (supplyMapper.selectGoodsBySupplyId(supplyId).size() != 0) {
            throw new RuntimeException("该供货商有商品，无法删除");
        }
        supplyMapper.deleteSupply(supplyId);
    }

    @Override
    public void updateSupply(Supply supply, int supplyId) {
        // 判断该供货商是否存在
        Supply supply1 = supplyMapper.selectById(supplyId);
        if (supply1 == null) {
            throw new RuntimeException("供货商不存在");
        }

        supplyMapper.updateSupply(supply);

    }

    @Override
    public Supply selectById(Integer id) {
        return supplyMapper.selectById(id);
    }

    @Override
    public void removeByIdList(ArrayList<Integer> ids) {
        // 遍历集合 删除含有商品的供货商
        ids.forEach(id -> {
            if (supplyMapper.selectGoodsBySupplyId(id).size() != 0) {
                ids.remove(id);
            }
        });
        supplyMapper.deleteByIdList(ids);
    }

    @Override
    public Workbook export(int pageNum, int pageSize, Supply supply) {
        PageHelper.startPage(pageNum, pageSize);
        List<Supply> list = supplyMapper.selectBySupplyId(supply);

        ExportParams params = new ExportParams("供应商信息表", "供应商表");
        Workbook workbook = ExcelExportUtil.exportExcel(params, Supply.class, list);


        return workbook;
    }

    @Override
    public void importExcel(MultipartFile multipartFile) {

        ImportParams params = new ImportParams();

        params.setTitleRows(1);
        params.setHeadRows(1);

        List<Member> list = null;
        try {
            list = ExcelImportUtil.importExcel(multipartFile.getInputStream(), Supply.class, params);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        supplyMapper.insertBatch(list);

    }
}
